/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.fzhucc.modules.elegant.controller;

import com.fzhucc.annotation.AnonymousAccess;
import com.fzhucc.annotation.Log;
import com.fzhucc.modules.elegant.domain.Elegant;
import com.fzhucc.modules.elegant.domain.dto.ElegantAddEditDto;
import com.fzhucc.modules.elegant.domain.dto.ElegantDto;
import com.fzhucc.modules.elegant.service.ElegantService;
import com.fzhucc.modules.elegant.domain.dto.ElegantQueryCriteria;
import com.fzhucc.modules.sciTechnologyDemand.domain.dto.SciTechnologyDemandDto;
import com.fzhucc.modules.sciTechnologyDemand.domain.dto.SciTechnologyDemandQueryCriteria;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
 * @author yyg
 * @date 2024-01-23
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "劳模风采展:管理")
@RequestMapping("/api/elegant")
public class ElegantController {

    private final ElegantService elegantService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('elegant:list')")
    public void exportElegant(HttpServletResponse response, ElegantQueryCriteria criteria) throws IOException {
        elegantService.download(elegantService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询劳模风采展")
    @ApiOperation("查询劳模风采展")
    @PreAuthorize("@el.check('elegant:list')")
    public ResponseEntity<Object> queryElegant(ElegantQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(elegantService.queryAll(criteria,pageable), HttpStatus.OK);
    }

    @AnonymousAccess
    @GetMapping(value = "/list")
    @Log("h5、小程序查询风采展示")
    @ApiOperation("h5、小程序查询风采展示")
    public ResponseEntity<Object> list(ElegantQueryCriteria criteria, Pageable pageable) {
        criteria.setIsShow(1);
        return new ResponseEntity<>(elegantService.queryAll(criteria, pageable), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增劳模风采展")
    @ApiOperation("新增劳模风采展")
    @PreAuthorize("@el.check('elegant:add')")
    public ResponseEntity<Object> createElegant(@Validated @RequestBody ElegantAddEditDto resources){
        return new ResponseEntity<>(elegantService.create(resources), HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改劳模风采展")
    @ApiOperation("修改劳模风采展")
    @PreAuthorize("@el.check('elegant:edit')")
    public ResponseEntity<Object> updateElegant(@Validated @RequestBody ElegantAddEditDto resources){
        elegantService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除劳模风采展")
    @ApiOperation("删除劳模风采展")
    @PreAuthorize("@el.check('elegant:del')")
    public ResponseEntity<Object> deleteElegant(@RequestBody Integer[] ids) {
        elegantService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @AnonymousAccess
    @GetMapping("/{id}")
    @Log("查询单个劳模风采展")
    @ApiOperation("查询单个劳模风采展")
    public ElegantDto queryInfoById(@PathVariable Integer id) {
        return elegantService.findById(id);
    }
}